<?php

namespace app\warehouse\logic;

use think\facade\Db;
use app\common\library\Excel;



class DetailedListLogic extends BasicLogic
{

    /**
     * @desc   仓库清单
     * @author zhangheng
     * @date   2024-12-11
     */
    public function getDetailedList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $create_time     = getIsset($params,'create_time');
        $where           = $order = [];
        
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }



        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        //排序
        $safe_quantity          = $params['safe_quantity'] ?? "";
        $on_way_quantity        = $params['on_way_quantity'] ?? "";
        $not_send_quantity      = $params['not_send_quantity'] ?? "";
        $using_quantity         = $params['using_quantity'] ?? "";
        $inventory_quantity     = $params['inventory_quantity'] ?? "";
        $all_inventory_quantity = $params['all_inventory_quantity'] ?? "";


        if($safe_quantity){
            $order['safe_quantity'] = $safe_quantity;
        }
        if($on_way_quantity){
            $order['on_way_quantity'] = $on_way_quantity;
        }
        if($not_send_quantity){
            $order['not_send_quantity'] = $not_send_quantity;
        }
        if($using_quantity){
            $order['using_quantity'] = $using_quantity;
        }
        if($inventory_quantity){
            $order['inventory_quantity'] = $inventory_quantity;
        }
        if($all_inventory_quantity){
            $order['all_inventory_quantity'] = $all_inventory_quantity;
        }

        if(!$order){
            $order['update_time'] = 'desc';
        }

        $res = Db::name('warehouse_spu_inventory')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];


        $on_way_quantity_total        = array_sum(array_column($list, 'inventory_quantity'));
        $not_send_quantity_total      = array_sum(array_column($list, 'not_send_quantity'));
        $using_quantity_total         = array_sum(array_column($list, 'using_quantity'));
        $inventory_quantity_total     = array_sum(array_column($list, 'inventory_quantity'));
        $all_inventory_quantity_total = array_sum(array_column($list, 'inventory_quantity'));

        $data = [
            'list'                         => $list,
            'total'                        => $res->total(),
            'on_way_quantity_total'        => $on_way_quantity_total,
            'not_send_quantity_total'      => $not_send_quantity_total,
            'using_quantity_total'         => $using_quantity_total,
            'inventory_quantity_total'     => $inventory_quantity_total,
            'all_inventory_quantity_total' => $all_inventory_quantity_total,
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库清单详情
     * @author zhangheng
     * @date   2024-12-11
     */
    public function find($params=[]){
        $id     = getIsset($params,'id');
        if(!$id) return   $this->jsonMessage(201,false,'ID不能为空!',null);
        $data    = Db::name('warehouse_spu_inventory')
            ->field("*")
            ->where('id',$id)
            ->find();
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }

    /**
     * @desc   仓库清单修改
     * @author zhangheng
     * @date   2024-12-11
     */
    public function edit($params=[]){
        $id                      = getIsset($params,'id');
        $safe_quantity           = getIsset($params,'safe_quantity');
        $on_way_quantity         = getIsset($params,'on_way_quantity');
        $not_send_quantity       = getIsset($params,'not_send_quantity');
        $using_quantity          = getIsset($params,'using_quantity');
        $inventory_quantity      = getIsset($params,'inventory_quantity');
        $all_inventory_quantity  = getIsset($params,'all_inventory_quantity');
        $product_name            = getIsset($params,'product_name');
        $product_images          = getIsset($params,'product_images');
        if(!$id) return     $this->jsonMessage(201,false,'ID不能为空!',null);
        $update_info = [
            'safe_quantity'          =>$safe_quantity,
            'on_way_quantity'        =>$on_way_quantity,
            'not_send_quantity'      =>$not_send_quantity,
            'using_quantity'         =>$using_quantity,
            'inventory_quantity'     =>$inventory_quantity,
            'all_inventory_quantity' =>$all_inventory_quantity,
            'product_name'           =>$product_name,
            'product_images'         =>$product_images,
            'update_time'            =>date('Y-m-d H:i:s',time()),
        ];
        Db::name('warehouse_spu_inventory')->where(['id'=>$id])->update($update_info);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }




    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 仓库清单导出
     * @author zhangheng
     */
    public function export($params = [])
    {
        $header = [];
        $header[] = ['SPU', 'spu'];
        $header[] = ['安全库存数量', 'safe_quantity'];
        $header[] = ['在途数量', 'on_way_quantity'];
        $header[] = ['未发数量', 'not_send_quantity'];
        $header[] = ['占用库存数量', 'using_quantity'];
        $header[] = ['可用库存数量', 'inventory_quantity'];
        $header[] = ['库存总数量', 'all_inventory_quantity'];
        $header[] = ['仓库', 'warehouse'];
        $header[] = ['创建时间', 'create_time'];
        $header[] = ['更新时间', 'update_time'];
        
        $spu             = getIsset($params,'spu');
        $where           = [];
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }
        $list = Db::name('warehouse_spu_inventory')
            ->where($where)
            ->order("id desc")
            ->select()->toArray();

        $tableName = "DetailedList";
        $fileName = "export_" . $tableName . "_" . time();
        return Excel::exportData($list, $header, $fileName, 'xlsx');
    }

    /**
     * @desc   import
     * @author zhagnheng
     * @date   2024-12-10
     */
    public function import($url){
        return json(DefaultResponse(200, false, '导入成功!', null));
    }






}